layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoComment = {
        id: 'BoCommentTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'headPhoto', title: '头像', align: 'center',
                templet: function (object) {
                    return '<img src="/layuimini/images/commhead/' + object.headPhoto + '" style="width: 28px;height: 28px;border-radius: 50%;">';
                }
            },
            {field: 'userName', title: '评论者昵称', align: 'center',
                templet: function (object) {
                    if(object.userName == '' || object.userName == null || object.userName == undefined){
                        return '-';
                    }else{
                        //副博主
                        if(object.isAdmin == 1 && object.type == 0){
                            return '<span style="color:#ff37d5">' + object.userName + "</span>";
                            //博主
                        }else if((object.isAdmin == 2 && object.type == 0)){
                            return '<span style="color:#FF5722">' + object.userName + "</span>";
                        }else{
                            return object.userName;
                        }
                    }
                }
            },
            {field: 'byUserName', title: '回复者昵称', align: 'center',
                templet: function (object) {
                    if(object.byUserName == '' || object.byUserName == null || object.byUserName == undefined){
                        return '-';
                    }else{
                        //副博主
                        if(object.isAdmin == 1 && object.type == 1){
                            return '<span style="color:#ff37d5">' + object.byUserName + "</span>";
                            //博主
                        }else if((object.isAdmin == 2 && object.type == 1)){
                            return '<span style="color:#FF5722">' + object.byUserName + "</span>";
                        }else{
                            return object.byUserName;
                        }
                    }
                }
            },
            {field: 'content', title: '内容', align: 'center',
                templet: function (object) {
                    if(object.content == '' || object.content == null || object.content == undefined){
                        return '-';
                    }else{
                        return object.content;
                    }
                }
            },
            {field: 'referContent', title: '引用原内容', align: 'center',
                templet: function (object) {
                    if(object.referContent == '' || object.referContent == null || object.referContent == undefined){
                        return '-';
                    }else{
                        return object.referContent;
                    }
                }
            },
            {field: 'classify', title: '评论分类', align: 'center',
                templet: function (object) {
                    var html = '';
                    if(object.classify == 0){
                        html = '<span class="layui-badge">文章</span>';
                    }else if(object.classify == 1){
                        html = '<span class="layui-badge layui-bg-orange">相册</span>';
                    }else if(object.classify == 2){
                        html = '<span class="layui-badge layui-bg-green">留言</span>';
                    }else if(object.classify == 3){
                        html = '<span class="layui-badge layui-bg-cyan">日记</span>';
                    }else if(object.classify == 4){
                        html = '<span class="layui-badge layui-bg-black">个人空间</span>';
                    }
                    return html;
                }
            },
            {field: 'type', title: '评论类型', align: 'center',
                templet: function (object) {
                    var html = '';
                    if(object.type == 0){
                        html = '<span class="layui-badge">评论</span>';
                    }else if(object.type == 1){
                        html = '<span class="layui-badge layui-bg-green">回复</span>';
                    }
                    return html;
                }
            },
            {field: 'time', title: '评论时间', align: 'center'},
            {field: 'praise', title: '赞数量', align: 'center'},
            {field: 'isRead', title: '阅读状态', align: 'center',
                templet: function (object) {
                    var html = '';
                    if(object.isRead == 0){
                        html = '<span class="layui-badge">未读</span>';
                    }else if(object.isRead == 1){
                        html = '<span class="layui-badge layui-bg-green">已读</span>';
                    }
                    return html;
                }
            },
            {field: 'isAdmin', title: '用户类型', align: 'center',
                templet: function (object) {
                    var html = '';
                    if(object.isAdmin == 0){
                        html = '<span class="layui-badge">游客</span>';
                    }else if(object.isAdmin == 1){
                        html = '<span class="layui-badge layui-bg-blue">副博主</span>';
                    } else if(object.isAdmin == 2){
                        html = '<span class="layui-badge layui-bg-green">博主</span>';
                    }
                    return html;
                }
            },
            {templet: '#stateBtn', width: 170, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoComment.id,
        url: '/web/boComment/recycleList',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击彻底删除按钮
     * @param id
     */
    BoComment.thoroughlyDelete = function(id){
        layer.open({
            title: '彻底删除评论/留言'
            ,content: '是否确定彻底删除该评论/留言？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boComment/thoroughlyDelete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoComment.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    /**
     * 点击恢复按钮
     * @param id
     */
    BoComment.restore = function(id){
        layer.open({
            title: '恢复评论/留言'
            ,content: '是否确定恢复该评论/留言？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boComment/restore';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoComment.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoComment.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'thoroughlyDelete') {
            BoComment.thoroughlyDelete(data.id);
        }else if(layEvent === 'restore'){
            BoComment.restore(data.id);
        }
    });

});